//
//  MainHomeVC.m
//  BMW
//
//  Created by up72-2 on 15/10/26.
//  Copyright © 2015年 up72-2. All rights reserved.
//

#import "MainHomeVC.h"
#import "SelectItemView.h"
#import "QuestionView.h"
#import "OffUpLoadView.h"
#import "ViewController.h"
@interface MainHomeVC ()
@property (weak, nonatomic) IBOutlet UILabel *showTitleLabel;
@property (weak, nonatomic) IBOutlet UIButton *selectBtn;
@property (weak, nonatomic) IBOutlet UIButton *resetBtn;
@property (weak, nonatomic) IBOutlet UIButton *questionBtn;
@property (weak, nonatomic) IBOutlet UIButton *uploadBtn;
@property (strong, nonatomic) SelectItemView *itemView;
@property (strong, nonatomic) QuestionView *questionView;
@property (strong, nonatomic) OffUpLoadView *uploadView;
@end

static NSString *versionURL;
@implementation MainHomeVC



- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"showSigmatureView" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [MTool storeCurrentState:@"在线"];
    
    [self allocUIStyle];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSigmatureView:) name:@"showSigmatureView" object:nil];
    
}

- (void)allocUIStyle
{
    popVC = [[ProductPopVC alloc] init];
    
    popOver = [[UIPopoverController alloc] initWithContentViewController:popVC];
    [popOver setPopoverContentSize:CGSizeMake(120.0,120.0)];
    
    [self.selectBtn setTitle:[MTool getUserName] forState:0];
    
    self.resetBtn.layer.borderColor = [UIColor blackColor].CGColor;
    self.resetBtn.layer.borderWidth = 1;
    
    self.layerView.backgroundColor = RGBACOLOR(102, 102, 102, 0.8);
    
    [self.versionLabel setText:[NSString stringWithFormat:@"版本号%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]]];
    
    self.uploadView.hidden = YES;
    
    if (!self.questionView) {
        self.questionView = [[[NSBundle mainBundle] loadNibNamed:@"QuestionView" owner:nil options:nil] objectAtIndex:0];
    }
    self.questionView.hidden = NO;
    self.questionView.frame = CGRectMake(0, 168, kScreenWidth, kScreenHeight-168);
    [self.view addSubview:self.questionView];
    
    
    [self.questionBtn setBackgroundImage:[UIImage imageNamed:@"commit_yes"] forState:0];
}



- (void)showSigmatureView:(NSNotification *)sender {
//    if (!self.signatureView) {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:sender.userInfo];
    SignatureView *signatureView = [[[NSBundle mainBundle] loadNibNamed:@"SignatureView" owner:nil options:nil] objectAtIndex:0];
    [signatureView setDataDic:dic];
    __weak typeof (self)weakSelf = self;
    signatureView.back = ^(){
        
        [weakSelf.uploadView handlePlist];
    };
    [self.view addSubview:signatureView];
//    }
}


- (IBAction)selectBtn:(UIButton *)sender
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)resetBtn:(UIButton *)sender
{
}

- (IBAction)question:(UIButton *)sender
{
    [sender setBackgroundImage:[UIImage imageNamed:@"commit_yes"] forState:0];
    [self.uploadBtn setBackgroundImage:[UIImage imageNamed:@"commit_no"] forState:0];
    
    
    self.uploadView.hidden = YES;
    
    if (!self.questionView) {
        self.questionView = [[[NSBundle mainBundle] loadNibNamed:@"QuestionView" owner:nil options:nil] objectAtIndex:0];
    }
    self.questionView.hidden = NO;
    self.questionView.frame = CGRectMake(0, 168, kScreenWidth, kScreenHeight-168);
    [self.view addSubview:self.questionView];
}

- (IBAction)uploadBtn:(UIButton *)sender
{
    [sender setBackgroundImage:[UIImage imageNamed:@"commit_yes"] forState:0];
    [self.questionBtn setBackgroundImage:[UIImage imageNamed:@"commit_no"] forState:0];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.questionView.hidden = YES;
    if (!self.uploadView) {
        self.uploadView = [[[NSBundle mainBundle] loadNibNamed:@"OffUpLoadView" owner:nil options:nil] objectAtIndex:0];
    }
    self.uploadView.hidden = NO;
    self.uploadView.frame = CGRectMake(0, 168, kScreenWidth, kScreenHeight-168);
    [self.view addSubview:self.uploadView];
}

- (IBAction)returnBtn:(UIButton *)sender
{
    self.aboutView.hidden = YES;
}
- (IBAction)updateBtn:(UIButton *)sender
{
    if (!netManager) {
        netManager = [[NetManager alloc]init];
    }
    netManager.delegate = self;
    netManager.loadingText = @"检查更新中";
    [netManager updateVersion];
}

- (IBAction)logoutBtn:(UIButton *)sender
{
    NSArray *vcArray = [self.navigationController viewControllers];
    for (NSInteger i = 0; i < [vcArray count];i++) {
        id temp = [vcArray objectAtIndex:i];
        if ([temp isKindOfClass:[ViewController class]]) {
            [self.navigationController popToViewController:temp animated:YES];
        }
    }
    
    //删除当前签到人信息
   
}

#pragma mark - 网络请求
- (void)requestStart:(NetManager *)request
{

}

- (void)requestError:(NetManager *)request error:(NSError *)error
{
    
}

- (void)requestDidFinished:(NetManager *)request result:(NSMutableDictionary *)result
{
    NSInteger status = [[NSString stringWithFormat:@"%@",[result objectForKey:@"state"]] integerValue];
    
    if (status != 1) {
        [MTool showMessage:[NSString stringWithFormat:@"%@",[result objectForKey:@"errorMsg"]] inView:self.view];
        return;
    }
    
    //处理数据
    NSString *nowVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString *serverVersion = [result objectForKey:@"newVersion"];
    versionURL = [result objectForKey:@"downloadUrl"];
    serverVersion = @"2.0";
    if (![nowVersion isEqualToString:serverVersion]) {
        [request removeLoadingView];
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"有新版本" message:@"有新版本可以更新，是否更新？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        }];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:versionURL]];
        }];
        [alertController addAction:cancleAction];
        [alertController addAction:sureAction];
        [self presentViewController:alertController animated:YES completion:nil];
    } else {
        [MTool showMessage:@"当前已经是最新版本" inView:self.view];
    }
}
- (IBAction)buttonAction:(UIButton *)sender
{
    NSArray *arr = [NSArray arrayWithObjects:[MTool getUserName],@"版本更新",@"注   销", nil];
    [popVC.myTableview setFrame:CGRectMake(0, 0, popOver.popoverContentSize.width, popOver.popoverContentSize.height)];
    
    
    [popVC resetTableData:arr];
    
    
    __weak typeof (self)weakSelf = self;
    __block UIPopoverController *tempPop = popOver;
    popVC.cellSelect = ^(NSString *str) {
        //判断
        if ([str isEqualToString:[MTool getUserName]]) {
            //获取用户名
            [weakSelf.selectBtn setTitle:str forState:0];
        }
        else if ([str isEqualToString:@"版本更新"])
        {
            //版本更新
            weakSelf.aboutView.hidden = NO;
            [weakSelf.view bringSubviewToFront:weakSelf.aboutView];
        }
        else if ([str isEqualToString:@"注   销"])
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否注销登录?" delegate:weakSelf cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        
        [tempPop dismissPopoverAnimated:YES];
    };
    
    [popOver presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}
@end
